package org.microdream.easy.deploy.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ice.server.comm.BaseServiceImpl;
import ice.server.comm.CommRepository;
import ice.server.comm.ParamBulider;
import ice.tool.ResultMap;
import org.apache.commons.lang3.StringUtils;
import org.microdream.easy.deploy.dao.DOrgRepository;
import org.microdream.easy.deploy.po.DOrg;
import org.microdream.easy.deploy.po.DRent;
import org.microdream.easy.deploy.utils.DateUtil;
import org.microdream.easy.deploy.utils.ListUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class DOrgService extends BaseServiceImpl<DOrg, Integer>{

    @Resource
    private DOrgRepository dOrgRepository;

    @Override
    public CommRepository<DOrg, Integer> getRepository() {

        return dOrgRepository;
    }

    @Override
    public Integer getIdenty(DOrg t) {
        return t.getId();
    }

    public Map add(DOrg dOrg){
        Integer flag = this.save(dOrg);
        return ResultMap.build().add(flag>0).get();
    }

    public Map upd(DOrg dOrg){
        Integer flag = this.save(dOrg);
        return ResultMap.build().update(flag>0).get();
    }

    public Map del(List<String> ids){
        List<DOrg> dOrgs = this.find(ParamBulider.build().add("orgCode_in", ListUtil.ListToString(ids,",")).get());
        dOrgs.forEach(dOrg -> {
            dOrg.setIsDelete(1);
        });
        return ResultMap.build().delete(this.save(dOrgs)).get();
    }

    /**
     * 得到所有可用机构的key-value键值对
     * */
    public JSONArray getOrgKeyValue(String rentCode,String keyName, String valueName){
        JSONArray jsonArray = new JSONArray();
        List<DOrg> dOrgs = this.find(ParamBulider.build().addEqual("rentCode",rentCode).addEqual("isDelete",0).get());
        jsonArray = build("0",dOrgs,"nodes",keyName,valueName);
        return jsonArray;
    }

    /**
     * 根据机构编码查询机构信息
     * */
    public DOrg searchByCode(String code){
        DOrg dOrg = this.findSingle(ParamBulider.build().addEqual("orgCode",code).get());

        if (!"0".equals(dOrg.getOrgFCode())) {
            DOrg dOrgF = this.findSingle(ParamBulider.build().addEqual("orgCode",dOrg.getOrgFCode()).get());
            dOrg.set("myName",StringUtils.isNotEmpty(dOrgF.getOrgName())?dOrgF.getOrgName():"无");
        }else {
            dOrg.set("myName","无");
        }
        if (dOrg.getUpdateTime()!=null) {
            dOrg.set("showUpdateTime", DateUtil.TimestampToString_1(dOrg.getUpdateTime()));
        }

        return dOrg;
    }

    private JSONArray build(String fcode,List<DOrg> dOrgs, String childrenKey,String keyName, String valueName){
        JSONArray jsonArray = new JSONArray();
        dOrgs.stream().forEach(dOrg -> {
            if(dOrg.getOrgFCode().equals(fcode)){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(StringUtils.isNotEmpty(keyName)?keyName:"myId",dOrg.getOrgCode());
                jsonObject.put(StringUtils.isNotEmpty(valueName)?valueName:"text",dOrg.getOrgName());
                JSONArray ja = build(dOrg.getOrgCode(),dOrgs, childrenKey,keyName,valueName);
                if(!ja.isEmpty()) {
                    jsonObject.put(childrenKey, ja);
                }
                jsonArray.add(jsonObject);
            }
        });
        return jsonArray;
    }

    public JSONArray searchOrgKeyValue(String rentCode,String keyName,String valueName){
        JSONArray jsonArray = new JSONArray();
        List<DOrg> dOrgs = this.find(ParamBulider.build().addEqual("rentCode",rentCode).addEqual("isDelete",0).get());
        dOrgs.forEach(dOrg -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(StringUtils.isNotEmpty(keyName)?keyName:"key",dOrg.getOrgCode());
            jsonObject.put(StringUtils.isNotEmpty(valueName)?valueName:"value",dOrg.getOrgName());
            jsonArray.add(jsonObject);
        });
        return jsonArray;
    }
}
